package jp.gr.java_conf.ryz.summarize.client.view;

import java.util.HashMap;
import java.util.Map;

import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecoration;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RadioButton;

public class ColorPaletPanel extends FlowPanel
{
	private static final String RadioGroupName = "RadioGroup_";
	private static final Map<String, String> _colors = new HashMap<String, String>();
	
	private Map<RadioButton, String> _colorIds = new HashMap<RadioButton, String>();
	private String _postNumber;
	private PostDecoration _decoration;
	
	static {
		_colors.put("color0", null);
		_colors.put("color1", "#DB7093");
		_colors.put("color2", "#800080");
		_colors.put("color3", "#800000");
		_colors.put("color4", "#FFA500");
		_colors.put("color5", "#008000");
		_colors.put("color6", "#0000FF");
		_colors.put("color7", "#FF0000");
	}

	public ColorPaletPanel(PostDecoration decoration, String postNumber)
	{
		super();
		
		_postNumber = postNumber;
		_decoration = decoration;
		
		init();
	}
	
	
	private void init()
	{
		add(createRadioButton("color0"));
		add(createRadioButton("color1"));
		add(createRadioButton("color2"));
		add(createRadioButton("color3"));
		add(createRadioButton("color4"));
		add(createRadioButton("color5"));
		add(createRadioButton("color6"));
		add(createRadioButton("color7"));
	}
	
	private RadioButton createRadioButton(String styleName)
	{
		final RadioButton radio = new RadioButton(RadioGroupName + _postNumber, "■");
		radio.addStyleName(styleName);
		radio.addValueChangeHandler(new ValueChangeHandler<Boolean>()
		{
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event)
			{
				if (!event.getValue())
					return;
				
				// FIXME : use radio.getElement().getStyle()
//				String color = radio.getElement().getStyle().getColor();
				String color = _colors.get(_colorIds.get(radio));
				_decoration.setColor(color);
			}
		});
		
		_colorIds.put(radio, styleName);
		
		return radio;
	}
}
